/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.market.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.market.entity.Event;
import com.kaaction.modules.market.dao.EventDao;
import com.kaaction.modules.market.entity.EventFlow;
import com.kaaction.modules.market.dao.EventFlowDao;
import com.kaaction.modules.market.entity.EventLottery;
import com.kaaction.modules.market.dao.EventLotteryDao;
import com.kaaction.modules.market.entity.EventPrize;
import com.kaaction.modules.market.dao.EventPrizeDao;

/**
 * 活动Service
 * @author zengbiao
 * @version 2017-04-22
 */
@Service
@Transactional(readOnly = true)
public class EventService extends CrudService<EventDao, Event> {

	@Autowired
	private EventFlowDao eventFlowDao;
	@Autowired
	private EventLotteryDao eventLotteryDao;
	@Autowired
	private EventPrizeDao eventPrizeDao;
	
	public Event get(String id) {
		Event event = super.get(id);
		if(event != null){
			List<EventFlow> flowList = eventFlowDao.findList(new EventFlow(event));
			if(flowList != null){
				event.setEventFlowList(flowList);
			}
			List<EventLottery> lotteryList = eventLotteryDao.findList(new EventLottery(event));
			if(lotteryList != null){
				event.setEventLotteryList(lotteryList);
			}
			List<EventPrize> prizeList = eventPrizeDao.findList(new EventPrize(event));
			if(prizeList != null){
				event.setEventPrizeList(prizeList);
			}
		}
		return event;
	}
	
	public Event getByProductId(String productId) {
		return super.dao.getByProductId(productId);
	}
	
	public List<Event> findList(Event event) {
		return super.findList(event);
	}
	
	public Page<Event> findPage(Page<Event> page, Event event) {
		return super.findPage(page, event);
	}
	
	@Transactional(readOnly = false)
	public void save(Event event) {
		super.save(event);
		for (EventFlow eventFlow : event.getEventFlowList()){
			if (eventFlow.getId() == null){
				continue;
			}
			if (EventFlow.DEL_FLAG_NORMAL.equals(eventFlow.getDelFlag())){
				if (StringUtils.isBlank(eventFlow.getId())){
					eventFlow.setEvent(event);
					eventFlow.preInsert();
					eventFlowDao.insert(eventFlow);
				}else{
					eventFlow.preUpdate();
					eventFlowDao.update(eventFlow);
				}
			}else{
				eventFlowDao.delete(eventFlow);
			}
		}
		for (EventLottery eventLottery : event.getEventLotteryList()){
			if (eventLottery.getId() == null){
				continue;
			}
			if (EventLottery.DEL_FLAG_NORMAL.equals(eventLottery.getDelFlag())){
				
				String eventLotteryImage = eventLottery.getImage();
				if(!StringUtils.isBlank(eventLotteryImage)){
					eventLotteryImage = eventLotteryImage.replace("|", Global.getPicPrefix());
					eventLottery.setImage(eventLotteryImage);
				}
				if (StringUtils.isBlank(eventLottery.getId())){
					eventLottery.setEvent(event);
					eventLottery.preInsert();
					eventLotteryDao.insert(eventLottery);
				}else{
					eventLottery.preUpdate();
					eventLotteryDao.update(eventLottery);
				}
			}else{
				eventLotteryDao.delete(eventLottery);
			}
		}
		for (EventPrize eventPrize : event.getEventPrizeList()){
			if (eventPrize.getId() == null){
				continue;
			}
			if (EventPrize.DEL_FLAG_NORMAL.equals(eventPrize.getDelFlag())){
				
				String eventPrizeImage = eventPrize.getImage();
				if(!StringUtils.isBlank(eventPrizeImage)){
					eventPrizeImage = eventPrizeImage.replace("|", Global.getPicPrefix());
					eventPrize.setImage(eventPrizeImage);
				}
				if (StringUtils.isBlank(eventPrize.getId())){
					eventPrize.setEventId(event);
					eventPrize.preInsert();
					eventPrizeDao.insert(eventPrize);
				}else{
					eventPrize.preUpdate();
					eventPrizeDao.update(eventPrize);
				}
			}else{
				eventPrizeDao.delete(eventPrize);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Event event) {
		super.delete(event);
		eventFlowDao.delete(new EventFlow(event));
		eventLotteryDao.delete(new EventLottery(event));
		eventPrizeDao.delete(new EventPrize(event));
	}
	
}